/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: AbstractRewardProcessor
 * Author:   zhouheng
 * Date:     2019/6/18 23:14
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zhouh.golden.activity.service.processor;

import com.zhouh.golden.activity.dao.ActDrawAwardItemMapper;
import com.zhouh.golden.activity.dao.ActDrawAwardRecordMapper;
import com.zhouh.golden.activity.domain.ActDrawAwardItem;
import com.zhouh.golden.activity.domain.ActDrawAwardRecord;
import com.zhouh.golden.activity.service.processor.ActivityDrawContext;
import com.zhouh.golden.activity.service.processor.RewardProcessor;
import com.zhouh.golden.activity.service.processor.exception.UnRewardException;
import com.zhouh.golden.common.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * 〈一句话功能简述〉<br> 
 * 〈模板模式 中奖处理方式由子类实现〉
 *
 * @author zhouheng
 * @create 2019/6/18
 * @since 1.0.0
 */
public abstract class AbstractRewardProcessor implements RewardProcessor<ActivityDrawContext> {
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void doReword(ActivityDrawContext activityDrawContext) {
        // 验证奖品数量及获取奖品明细
        beforeProcessor(activityDrawContext);

        // 处理发放奖品
        processor(activityDrawContext);

        //发送短信
        afterProcessor(activityDrawContext);
    }

    /**
     * @description: 验证奖品数量及获取奖品明细
     * @date: 2019/6/18 23:24
     * @author:zhouheng
     */
    protected void beforeProcessor(ActivityDrawContext activityDrawContext){
        //判断奖项当天发放总数以及整个活动过程中的总数：total_num、day_total_num
        if (validDayAwardNum(activityDrawContext) && validTotalAwardNum(activityDrawContext)){
            ActDrawAwardItem actDrawAwardItem = activityDrawContext.getActDrawAwardItem();

            Example example = new Example(ActDrawAwardItem.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id", activityDrawContext.getActDrawAwardItem().getId());
            actDrawAwardItem.setDayTotalNum(actDrawAwardItem.getDayTotalNum() - 1);
            actDrawAwardItem.setTotalNum(actDrawAwardItem.getTotalNum() - 1);
            actDrawAwardItem.setId(actDrawAwardItem.getId());

            // 扣减库存
            ActDrawAwardItemMapper actDrawAwardItemMapper = (ActDrawAwardItemMapper) SpringContextUtils.getBean("actDrawAwardItemMapper");
            int result = actDrawAwardItemMapper.updateActivityStock(actDrawAwardItem);
        } else{
            throw new UnRewardException("当前奖项已达到当日最大限制，自动选中六等奖'谢谢惠顾'");
        }
    }

    protected abstract void afterProcessor(ActivityDrawContext activityDrawContext);

    /**
     * @description: 校验总库存量师傅正常
     * @date: 2019/6/19 0:04
     * @author:zhouheng
     */
    protected boolean validTotalAwardNum(ActivityDrawContext activityDrawContext){
        // 总库存量是否大于0
        int totalNum = activityDrawContext.getActDrawAwardItem().getTotalNum();
        if (totalNum > 0){
            return true;
        }

        return false;
    }

    /**
     * @description: 校验当日库存量是否正常
     * @date: 2019/6/19 0:02
     * @author:zhouheng
     */
    protected boolean validDayAwardNum(ActivityDrawContext activityDrawContext){
        // 当日总库存量是否大于0
        int dayTotalNum = activityDrawContext.getActDrawAwardItem().getDayTotalNum();
        if (dayTotalNum > 0){
            return true;
        }

        return false;
    }

    /**
     * @description: 奖品发放处理
     * @date: 2019/6/18 23:19
     * @author:zhouheng
     */
    public abstract void processor(ActivityDrawContext activityDrawContext);

    /**
     * @description: 保存数据
     * @date: 2019/6/19 20:59
     * @author:zhouheng
     */
    public void saveDrawAwardRecord(ActivityDrawContext activityDrawContext) {
        ActDrawAwardRecord actDrawAwardRecord = new ActDrawAwardRecord();
        actDrawAwardRecord.setLevel(activityDrawContext.getActDrawAwardItem().getLevel());
        actDrawAwardRecord.setUserId(activityDrawContext.getActivityTurntableDrawReq().getUserId());
        actDrawAwardRecord.setName(activityDrawContext.getUserQueryResponse().getUsername());
        actDrawAwardRecord.setMobile(activityDrawContext.getUserQueryResponse().getMobile());
        actDrawAwardRecord.setAwardName(activityDrawContext.getActDrawAward().getAwardName());
        actDrawAwardRecord.setCreateTime(new Date());

        ActDrawAwardRecordMapper actDrawAwardRecordMapper = (ActDrawAwardRecordMapper) SpringContextUtils.getBean("actDrawAwardRecordMapper");
        actDrawAwardRecordMapper.insert(actDrawAwardRecord);
    }

}